package com.example.weatherview.view.a1

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.Shader
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.example.weatherview.R
import kotlin.math.roundToInt

class AirView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {



    private val mCursorBitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_cursor)

    private var mCursorHeadHeight = dp2Px(context,8f).toFloat()
    private var mCursorWof2 = mCursorBitmap.width/2f


    private var mLineHeight = dp2Px(context,18F).toFloat()

    private var mRound = dp2Px(context,2f).toFloat()



    private val mLinePaint = Paint().apply {
        style = Paint.Style.FILL
    }


    private val mTextPaint = Paint().apply {
        //color=Color.parseColor("#B8FFFFFF")
        color=Color.BLACK
        textAlign = Paint.Align.LEFT
        textSize =sp2Px(context,12F).toFloat()
    }

    private val mMax = 300


    private var mCurAir = 50



    private lateinit var mLinearGradient:LinearGradient

    private val mRectF = RectF()


    private val mColors = intArrayOf(
        Color.parseColor( "#44EE35"),
        Color.parseColor( "#FFD600"),
        Color.parseColor( "#FFAA2B"),
        Color.parseColor( "#DF0A0A"),
        Color.parseColor( "#910F0F")
    )

    private val mPos = arrayListOf(
        0F,
        0.16F,
        0.32F,
        0.48F,
        1f,
    ).toFloatArray()


    private var mTextHeight = 0

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mLinearGradient=LinearGradient(0F,h.toFloat(),w.toFloat(),h.toFloat(),mColors,null,Shader.TileMode.CLAMP)
        mLinePaint.shader = mLinearGradient
        mRectF.apply {
            left=mCursorWof2
            right = w.toFloat()-mCursorWof2
            top= mCursorHeadHeight
            bottom = (mLineHeight+mCursorHeadHeight)
        }

        val f = Rect()
        mTextPaint.getTextBounds("0",0,1,f)
        mTextHeight = f.height()
        Log.d("66", "onSizeChanged: $mTextHeight")
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val w = MeasureSpec.getSize(widthMeasureSpec)
        val h = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(w,h)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            //线
            drawRoundRect(mRectF, mRound, mRound, mLinePaint)

            //游标
            val realWidth = width - mCursorBitmap.width
            val airMargin = mCurAir*(realWidth / mMax.toFloat())
            drawBitmap(mCursorBitmap,airMargin,0F,mLinePaint)

            //范围
            drawMaxMin()
        }
    }



    private fun Canvas.drawMaxMin(){
        val incWidth = mCursorWof2
        val textOffset = dp2Px(context, 2f)
        val dy = mCursorHeadHeight+mLineHeight+mTextHeight+textOffset


        mTextPaint.textAlign = Paint.Align.LEFT
        drawText("0",incWidth,dy,mTextPaint)

        val maxX = width- incWidth
        mTextPaint.textAlign = Paint.Align.RIGHT
        drawText("$mMax",maxX,dy,mTextPaint)
    }


    private fun dp2Px(context: Context, dp: Float): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density).roundToInt()
    }

    private fun sp2Px(context: Context, sp: Float): Int {
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        return Math.round(sp * scaledDensity)
    }


    fun setAirValue(value:Int){
        mCurAir = if (mCurAir>mMax){
            mMax
        }else{
            value
        }
        invalidate()
    }
}